{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-03-25T02:10:58.882930Z",
     "start_time": "2025-03-25T02:10:58.834610Z"
    }
   },
   "source": "import numpy as np",
   "outputs": [],
   "execution_count": 1
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-25T02:10:58.896875Z",
     "start_time": "2025-03-25T02:10:58.889943Z"
    }
   },
   "cell_type": "code",
   "source": [
    "t = np.arange(24).reshape(4, 6)\n",
    "t = t.clip(10, 18)  #实现把小于10的元素替换为10，大于18的元素替换为18\n",
    "t"
   ],
   "id": "514876061da1780a",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[10, 10, 10, 10, 10, 10],\n",
       "       [10, 10, 10, 10, 10, 11],\n",
       "       [12, 13, 14, 15, 16, 17],\n",
       "       [18, 18, 18, 18, 18, 18]])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 2
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-25T02:10:59.022656Z",
     "start_time": "2025-03-25T02:10:59.017129Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 三目运算符的实现\n",
    "score = np.array([[80, 88], [82, 81], [75, 81]])\n",
    "result = np.where(score > 80, '及格', '不及格')\n",
    "result"
   ],
   "id": "f8463b962218968a",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([['不及格', '及格'],\n",
       "       ['及格', '及格'],\n",
       "       ['不及格', '及格']], dtype='<U3')"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 3
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-25T03:27:55.827357Z",
     "start_time": "2025-03-25T03:27:55.791573Z"
    }
   },
   "cell_type": "code",
   "source": [
    "a = np.array([[1, 2, 3], [4, 5, 6]])\n",
    "\n",
    "print(\"第一个数组\")\n",
    "print(a)\n",
    "print(\"\\n\")\n",
    "\n",
    "print(\"向数组中添加元素\")\n",
    "print(np.append(a, [7, 8, 9]))\n",
    "print(\"\\n\")\n",
    "\n",
    "print(\"向数组中添加元素，并指定轴\")\n",
    "print(np.append(a, [[7, 8, 9]], axis=0))\n"
   ],
   "id": "687c1b099eebd4b8",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第一个数组\n",
      "[[1 2 3]\n",
      " [4 5 6]]\n",
      "\n",
      "\n",
      "向数组中添加元素\n",
      "[1 2 3 4 5 6 7 8 9]\n",
      "\n",
      "\n",
      "向数组中添加元素，并指定轴\n",
      "[[1 2 3]\n",
      " [4 5 6]\n",
      " [7 8 9]]\n"
     ]
    }
   ],
   "execution_count": 6
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 前缀求和",
   "id": "9c570f0c129e4e74"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-25T03:28:31.859652Z",
     "start_time": "2025-03-25T03:28:31.854222Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "\n",
    "arr = np.array([1, 2, 3, 4, 5])\n",
    "prefix_sum = arr.cumsum()\n",
    "print(prefix_sum)\n",
    "\n",
    "\n",
    "arr_2d = np.array([[1, 2, 3], [4, 5, 6]])\n",
    "prefix_sum_2d = arr_2d.cumsum()\n",
    "print(prefix_sum_2d)"
   ],
   "id": "cf16b3e1ef3083d3",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 1  3  6 10 15]\n",
      "[ 1  3  6 10 15 21]\n"
     ]
    }
   ],
   "execution_count": 8
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
